#ifndef __LCD_H
#define __LCD_H
//################################################
/** \file lcd.c
  * \author Andy Koppe
  * \brief KS0713 LCD driver
  */

enum {LCD_PAGES = 8, LCD_ROWS = 64, LCD_COLS = 128};

// initialise LCD interface
static void lcd_init(void) ;
// reset LCD
static void lcd_reset(void);

// write data to LCD
void lcd_write(unsigned char b) ;

/* NO READING JUST NOW
// read data from LCD
uint8 
lcd_read(void) 
{
  LCD_CO |= 1<<LCD_RS;
  LCD_CO &= ~(1<<LCD_RD);
  uint8 b = LCD_DI;
  LCD_CO |= 1<<LCD_RD;
  return b;
}

void
lcd_xor(uint8 b) 
{
  //lcd_read();
  //lcd_write(lcd_read() ^ b);
  LCD_CO |= 1<<LCD_RS;
  LCD_CO &= ~(1<<LCD_RD);
  LCD_CO |= 1<<LCD_RD;
  LCD_CO &= ~(1<<LCD_RD);
  LCD_CO |= 1<<LCD_RD;
  LCD_CO &= ~(1<<LCD_RD);
  uint8 a = LCD_DI;
  LCD_CO |= 1<<LCD_RD;
  LCD_DO = a ^ b;
  LCD_DD = 0xff;
  LCD_CO &= ~(1<<LCD_WR);
  LCD_CO |= 1<<LCD_WR;
  LCD_DD = 0;
  LCD_DO = 0;
} 

void
lcd_mask(uint8 on, uint8 off) 
{
  //lcd_read();
  //lcd_write(lcd_read() ^ b);
  LCD_CO |= 1<<LCD_RS;
  LCD_CO &= ~(1<<LCD_RD);
  LCD_CO |= 1<<LCD_RD;
  LCD_CO &= ~(1<<LCD_RD);
  LCD_CO |= 1<<LCD_RD;
  LCD_CO &= ~(1<<LCD_RD);
  uint8 b = LCD_DI;
  LCD_CO |= 1<<LCD_RD;
  LCD_DO = (b & off) ^ on;
  LCD_DD = 0xff;
  LCD_CO &= ~(1<<LCD_WR);
  LCD_CO |= 1<<LCD_WR;
  LCD_DD = 0;
  LCD_DO = 0;
}
 */


// write a command to LCD
static void lcd_command(unsigned char b) ;
/* NO RAEDING FOR NOW
// read status from LCD
static uint8 
lcd_status(void) 
{
  LCD_CO &= ~(1<<LCD_RS);
  LCD_CO &= ~(1<<LCD_RD);
  uint8 b = LCD_DI;
  LCD_CO |= 1<<LCD_RD;
  return b;
}*/

static  void lcd_soft_reset(void);
static  void lcd_display(unsigned char b);

static  void lcd_set_voltage(unsigned char b);
static  void lcd_set_resistor(unsigned char b);
static  void lcd_power_control(unsigned char b);

static  void lcd_set_shl(unsigned char b);
static  void lcd_set_adc(unsigned char b);
static  void lcd_set_bias(unsigned char b);
static  void lcd_set_black(unsigned char b);
static  void lcd_set_reverse(unsigned char b);

static  void lcd_set_modify_read(void);
static  void lcd_reset_modify_read(void);

static  void lcd_set_initial_line(unsigned char b);
void lcd_set_page(unsigned char p);
void lcd_set_col(unsigned char c) ;
void lcd_set_pos(unsigned char p, unsigned char c);

void lcd_setup(void);

//#############################################

void lcd_print_char(char c);
void lcd_print_string(char *s);

void lcd_print_cstring(const char* s);
void lcd_print_cchar(const char c);

void lcd_do_setup(void);
void lcd_print_rle_bitmap(const char *s);

void pause(unsigned char loops);
void SetPins(unsigned char c);

void lcd_fill_rect(unsigned char p,unsigned char c,unsigned char h, unsigned char w, unsigned char pat);
void lcd_clear_rect(unsigned char p,unsigned char c,unsigned char h, unsigned char w) ;
void lcd_clear(void);

void lcd_print_full_bitmap(const char *s);

void lcd_maybe_get_pos(unsigned char *x, unsigned char *y);

#endif